import glob
import sys
import os
from subprocess import call

# sampleName = sys.argv[1]
# workdir = sys.argv[2]  # This dir has X.CDNA.list,X.TCR.list,X.BCR.list
# flag = sys.argv[3]  #f1,TCR; f2,BCR; f3,TCRDG
# cluflag = sys.argv[4] #T or F
# cmdflag = sys.argv[5] #T or F
# marGeneflag = sys.argv[6]  #T or F

sampleName = "p110"
workdir = "/media/hsmy/wanghao_18T/biology/tcr/p110/tumor"  # This dir has X.CDNA.list,X.TCR.list,X.BCR.list
flag = "f1"  #f1,TCR; f2,BCR; f3,TCRDG
cluflag = "T" #T or F
cmdflag = "T" #T or F
marGeneflag = "T"  #T or F

#finish in 2021.11.10
#auto to analysis Single Cell

cmdList = []
#setting
codedir = "/home/hsmy/code/ik/TCR/singlecell"
GeneList = "%s/GeneExpTable/HumanGeneList-0927.xls" % (codedir)
python = "/home/hsmy/anaconda3/envs/py27/bin/python"

def getNeedFiles(fileDict,fileChoList,filedir):

    getFileDict={}
    for i in fileChoList:
        if i in fileDict:
            fpath = "%s/*%s" % (filedir,fileDict[i])
            temp = glob.glob(fpath)
            if len(temp) == 1:
                getFileDict[i] = temp[0]
            elif len(temp) == 0:
                getFileDict[i] = "F"
            else:
                print("files number error in:",fpath,len(temp))
                getFileDict[i] = temp[0]
        else:
            print("error:",i)
            return False
    return getFileDict


fileDict = {"CDNADir":"CDNA.list","BCRDir":"BCR.list","TCRDir":"TCR.list","TCRDGDir":"TCRDG.list",
            "TCRinfo1":"TCRinfo1.xls","TCRinfo1Bar":"TCRinfo1.xls.StandTCR.Bar","TCRinfo2":"BarTCRInfo.xls",
            "BCRinfo1":"BCRinfo1.xls","BCRinfo1Bar":"BCRinfo1.xls.StandBCR.Bar","BCRinfo2":"BarBCRInfo.xls",
            "GCMat":"GCMat.RData","BCRGCMat":"BCR.GCMat.RData","GCMatTCRFBar":"Barcode.txt","GCMatBCRFBar":"BCR.Barcode.txt",
            "BarCluster":"BarCluster.xls",
            "CITEBarcodeExp":"CITEExp.Mat.RData",
            "TCRChainsInfo":"TCRChains.Info.xls",
            "RawTCRSeq":"RawTCRSeq.fa","TCRFaSeq":"TCRFaSeq.xls","TCRExtSeq":"TCRExtSeq.xls"}

filedir = workdir


#
#def CleanTable(FileList,rawdir,desdir):
#    
#    for i in FileList:
#        cmd = "cp %s %s"

if flag == "f1": #TCR + CDNA

    #TCRana1
    fileChoList = ["TCRDir"]
    Out1 = "%s/%s.TCRinfo1.xls" % (workdir,sampleName)
    CSDict = getNeedFiles(fileDict,fileChoList,filedir)
    #cmd = "Rscript %s/XCRAna/TCRana1.old.R %s %s 1" % (codedir,CSDict["TCRDir"],Out1)   #suit for TCR old version
    cmd = "Rscript %s/XCRAna/TCRana1.R %s %s 1" % (codedir,CSDict["TCRDir"],Out1)
    call(cmd,shell=True)
    cmdList.append(cmd)

    #get GCMat
    fileChoList = ["CDNADir","TCRinfo1Bar"]
    CSDict = getNeedFiles(fileDict,fileChoList,filedir)
    cmd = "Rscript %s/GCMat/getFinalGCMat.R %s %s %s 3 F T %s" % (codedir,workdir,CSDict["CDNADir"],sampleName,CSDict["TCRinfo1Bar"])
    call(cmd,shell=True)
    cmdList.append(cmd)

    #get TCR raw seq
    fileChoList = ["TCRDir"]
    CSDict = getNeedFiles(fileDict,fileChoList,filedir)
    Out1 = "%s/%s.RawTCRSeq.fa" % (workdir,sampleName)
    #cmd = "Rscript %s/XCRAna/getXCRconSeq.old.R %s %s 1" % (codedir,CSDict["TCRDir"],Out1)  #suit for TCR old version
    cmd = "Rscript %s/XCRAna/getXCRconSeq.R %s %s 1" % (codedir,CSDict["TCRDir"],Out1)
    call(cmd,shell=True)
    cmdList.append(cmd)

    #TCRana2
    fileChoList = ["GCMatTCRFBar","TCRinfo1","RawTCRSeq"]
    CSDict = getNeedFiles(fileDict,fileChoList,filedir)
    cmd = "%s %s/XCRAna/XCRana2.py %s %s %s %s 1 %s" % (python,codedir,CSDict["TCRinfo1"],CSDict["GCMatTCRFBar"],sampleName,workdir, CSDict["RawTCRSeq"])
    call(cmd,shell=True)
    cmdList.append(cmd)

    #design TCR pri
    fileChoList = ["TCRFaSeq"]
    CSDict = getNeedFiles(fileDict,fileChoList,filedir)
    Out1 = "%s/%s.TCRExtSeq.xls" % (workdir,sampleName)
    cmd = "%s %s/TCRPriDesign/TCRSeqExt.py %s %s" % (python,codedir,CSDict["TCRFaSeq"],Out1)
    call(cmd,shell=True)
    cmdList.append(cmd)
 
    fileChoList = ["TCRExtSeq"]
    CSDict = getNeedFiles(fileDict,fileChoList,filedir)
    priDB = "%s/TCRPriDesign/DB" % (codedir)
    cmd = "%s %s/TCRPriDesign/TCRPriDesign.AddAdj.py %s %s %s %s" % (python,codedir,priDB,sampleName,workdir,CSDict["TCRExtSeq"])
    call(cmd,shell=True)
    cmdList.append(cmd)


    #cluster + final GeneExp
    if cluflag == "T": #cluster
        fileChoList = ["GCMat"]
        CSDict = getNeedFiles(fileDict,fileChoList,filedir)
        #no regress
        cmd = "Rscript %s/Cluster/ClusterSeurat3.R %s %s %s F %s" % (codedir,CSDict["GCMat"],sampleName,workdir,marGeneflag)
        call(cmd,shell=True)
        cmdList.append(cmd)

        fileChoList = ["TCRinfo2","GCMat","BarCluster","CITEBarcodeExp"]
        CSDict = getNeedFiles(fileDict,fileChoList,filedir)
        Out1 = "%s/%s.TCRBarGeneExp.xls" % (workdir,sampleName)
        #TCR + (TCRCD4/8,double),use cluster barcode
        cmd = "Rscript %s/GeneExpTable/getGeneExpTable.R %s %s 1 %s %s 2 %s %s" % (codedir,CSDict["GCMat"],CSDict["TCRinfo2"],GeneList,CSDict["BarCluster"],CSDict["CITEBarcodeExp"],Out1)
        call(cmd,shell=True)
        cmdList.append(cmd)

    else: #no cluster
        fileChoList = ["TCRinfo2","GCMat","CITEBarcodeExp"]
        CSDict = getNeedFiles(fileDict,fileChoList,filedir)
        Out1 = "%s/%s.TCRBarGeneExp.xls" % (workdir,sampleName)
        cmd = "Rscript %s/GeneExpTable/getGeneExpTable.R %s %s 1 %s F 1 %s %s" % (codedir,CSDict["GCMat"],CSDict["TCRinfo2"],GeneList,CSDict["CITEBarcodeExp"],Out1)
        call(cmd,shell=True)
        cmdList.append(cmd)

    #sand results
    #sandir = "%s/sand" % (workdir)
    #cmd = "%s %s/Auto-SCSend.py %s %s" % (python,codedir,sandir)
    #call(cmd,shell=True)
    #cmdList.append(cmd)

elif flag == "f2": #BCR + CDNA
    fileChoList = ["BCRDir"]
    Out1 = "%s/%s.BCRinfo1.xls" % (workdir,sampleName)
    CSDict = getNeedFiles(fileDict,fileChoList,filedir)
    cmd = "Rscript %s/XCRAna/BCRana1.R %s %s 1" % (codedir,CSDict["BCRDir"],Out1)
    print(cmd)
    call(cmd,shell=True)
    cmdList.append(cmd)

    fileChoList = ["CDNADir","BCRinfo1Bar"]
    CSDict = getNeedFiles(fileDict,fileChoList,filedir)
    BCRName = "%sBCR" % (sampleName)
    cmd = "Rscript %s/GCMat/getFinalGCMat.R %s %s %s 3 F T %s" % (codedir,workdir,CSDict["CDNADir"],BCRName,CSDict["BCRinfo1Bar"])
    print(cmd)
    call(cmd,shell=True)
    cmdList.append(cmd)
   
    fileChoList = ["BCRinfo1","GCMatBCRFBar"]
    CSDict = getNeedFiles(fileDict,fileChoList,filedir)
    cmd = "%s %s/XCRAna/XCRana2.py %s %s %s %s 2 F" % (python,codedir,CSDict["BCRinfo1"],CSDict["GCMatBCRFBar"],sampleName,workdir)
    print(cmd)
    call(cmd,shell=True)
    cmdList.append(cmd)

    fileChoList = ["BCRGCMat","BCRinfo2","BarCluster","CITEBarcodeExp"]
    CSDict = getNeedFiles(fileDict,fileChoList,filedir)
    Out1 = "%s/%s.BCRBarGeneExp.xls" % (workdir,sampleName)
    cmd = "Rscript %s/GeneExpTable/getGeneExpTable.R %s %s 2 %s %s 1 %s %s" % (codedir,CSDict["BCRGCMat"],CSDict["BCRinfo2"],GeneList,CSDict["BarCluster"],CSDict["CITEBarcodeExp"],Out1)
    print(cmd)
    call(cmd,shell=True)
    cmdList.append(cmd)

elif flag == "f3":  # TCR deta gama
    print("TCR DG!")
    #fileChoList = ["CDNADir","TCRDGDir"]
    #Out1 = "%s/%s.TCRinfo1.xls" % (workdir,sampleName)
    #CSDict = getNeedFiles(fileDict,fileChoList,filedir)

    #cmd = "Rscript /data2/users/dongxingjun/Human_SC_Ana/TCRdg/TCRDGana1.R %s %s 1" % (CSDict["TCRDGDir"],Out1)
    #call(cmd,shell=True)
    #cmdList.append(cmd)

    #cmd = "Rscript /home/xingjun/database/code/SingleCell1/GCMat/getFinalGCMat.R %s %s %s 1 F F F" % (workdir,CSDict["CDNADir"],sampleName)
    #call(cmd,shell=True)
    #cmdList.append(cmd)

    #cmd = "python /home/xingjun/database/code/SingleCell1/XCRAna/XCRana2.py %s F %s %s 1 F" % (Out1,sampleName,workdir)
    #call(cmd,shell=True)
    #cmdList.append(cmd)

    #fileChoList = ["TCRinfo2","GCMat"]
    #CSDict = getNeedFiles(fileDict,fileChoList,filedir)
    #Out2 = "%s/%s.TCRDGBarGeneExp.xls" % (workdir,sampleName)
    #cmd = "Rscript /home/xingjun/database/code/SingleCell1/GeneExpTable/getGeneExpTable.R %s %s 1 /home/xingjun/database/code/SingleCell1/GeneExpTable/GeneList/GeneList0927.xls F 1 F %s" % (CSDict["GCMat"],CSDict["TCRinfo2"],Out2)
    #call(cmd,shell=True)
    #cmdList.append(cmd)

#cmd
cmdsh = "%s/run_%s_%s_auto.sh" % (workdir,sampleName,flag)
w=open(cmdsh,mode="w+")
for i in cmdList:
    i += "\n"
    w.write(i)
w.close()

#cmd end.
